package com.qywx.controller;

import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.qywx.entity.ExportTagEntity;
import com.qywx.entity.ExportTagTemplateEntity;
import com.qywx.entity.TagEntity;
import com.qywx.service.TagService;
import com.qywx.utils.EasyPoiExcelUtil;
import com.qywx.utils.R;
import com.qywx.utils.WebExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TagController
 * @Description: TODO
 * @Author wmj
 * @Date 2020/3/9
 * @Version V1.0
 **/
@Api(value = "标签接口管理", tags = {"标签接口管理"})
@RestController
@RequestMapping("/tag")
public class TagController {

    @Autowired
    private TagService tagService;

    /**
     * @MethodName: save
     * @Description: 增加标签
     * @Param: [tagEntity]
     * @Return: com.qywx.utils.R
     * @Author: wmj
     * @Date: 2020/3/9
     **/
    @PostMapping("/save")
    @ApiOperation(value = "新增标签", notes = "tagname,标签名称")
    /*@ApiImplicitParams({
            @ApiImplicitParam(name = "tagname", required = true, value = "标签名称", paramType = "query", dataType = "String")
    })*/
    public R save(@RequestBody TagEntity tagEntity){
        return tagService.saveTag(tagEntity);
    }

    /**
     * @MethodName: updateTag
     * @Description: 修改标签
     * @Param: [tagEntity]
     * @Return: com.qywx.utils.R
     * @Author: wmj
     * @Date: 2020/3/9
     **/
    @PostMapping("/update")
    @ApiOperation(value = "修改标签", notes = "tagid，标签id")
    public R updateTag(@RequestBody TagEntity tagEntity){
        return tagService.updateTag(tagEntity);
    }

    @GetMapping("/find/name")
    @ApiOperation(value = "根据名称模糊查询标签", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", required = true, value = "标签名称", paramType = "query", dataType = "String")
    })
    public R findTagByName(@RequestParam("name") String name){
        return tagService.findTagByName(name);
    }
    /**
     * @MethodName: delTag
     * @Description: 删除标签
     * @Param: [tagid]
     * @Return: com.qywx.utils.R
     * @Author: wmj
     * @Date: 2020/3/9
     **/
    @GetMapping("/delete")
    @ApiOperation(value = "删除标签", notes = "tagid,标签id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tagid", required = true, value = "标签id", paramType = "query", dataType = "String")
    })
    public R delTag(@RequestParam("tagid") Long tagid){
        return tagService.delTag(tagid);
    }

    /**
     * @MethodName: getTagUser
     * @Description: 获取标签成员
     * @Param: [tagid]
     * @Return: com.qywx.utils.R
     * @Author: wmj
     * @Date: 2020/3/9
     **/
    @GetMapping("/find/user")
    @ApiOperation(value = "获取标签成员", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tagid", required = true, value = "标签id", paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "page", required = true, value = "当前页码", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", required = true, value = "每页显示条数", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "depid", required = true, value = "每页显示条数", paramType = "query", dataType = "int")
    })
    public R getTagUser(@RequestParam("tagid") Long tagid,@RequestParam("page") int page,@RequestParam("size") int size,@RequestParam("depid") int depid){
        return tagService.getTagUser(tagid,page,size,depid);
    }

    /**
     * @MethodName: saveTagUser
     * @Description: 增加标签成员
     * @Param: [tagid,userids]
     * @Return: com.qywx.utils.R
     * @Author: wmj
     * @Date: 2020/3/9
     **/
    @GetMapping("/save/tag/user")
    @ApiOperation(value = "增加标签成员,增加多个成员，成员id以逗号隔开", notes = "tagid,userids")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tagid", required = true, value = "标签id", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "userids", required = true, value = "用户id", paramType = "query", dataType = "String")
    })
    public R saveTagUser(@RequestParam("tagid") Long tagid,@RequestParam("userids") String userids){
        return tagService.saveTagUser(tagid,userids);
    }

    @GetMapping("/save/tag/department")
    @ApiOperation(value = "增加部门人员标签列表，部门id以逗号隔开", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tagid", required = true, value = "标签id", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "departmentIds", required = true, value = "部门id", paramType = "query", dataType = "String")
    })
    public R saveTagDepartment(@RequestParam("tagid") Long tagid,@RequestParam("departmentIds") String departmentIds){
        return tagService.saveTagDepartment(tagid,departmentIds);
    }

    /**
     * @MethodName: delTagUser
     * @Description: 删除标签成员
     * @Param: [tagid,userids]
     * @Return: com.qywx.utils.R
     * @Author: wmj
     * @Date: 2020/3/9
     **/
    @GetMapping("/delete/tag/user")
    @ApiOperation(value = "删除标签成员，删除多个成员，userid以逗号隔开", notes = "tagid：标签id，userids：用户id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tagid", required = true, value = "标签id", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "userids", required = true, value = "用户id", paramType = "query", dataType = "String")
    })
    public R delTagUser(@RequestParam("tagid") Long tagid,@RequestParam("userids") String userids){
        return tagService.delTagUser(tagid,userids);
    }

    /**
     * @MethodName: geTagList
     * @Description: 获取标签列表
     * @Param: []
     * @Return: com.qywx.utils.R
     * @Author: wmj
     * @Date: 2020/3/9
     **/
    @ApiOperation(value = "获取标签列表", notes = "")
    @GetMapping("/find/tag")
    public R geTagList(){
        return tagService.geTagList();
    }

    @ApiOperation(value = "异步检测", notes = "")
    @GetMapping("/asynchronous/detection")
    public R asynchronousDetection(){
        return tagService.asynchronousDetection();
    }

    @GetMapping("/synchronize")
    @ApiOperation(value = "异步检测后同步", notes = "")
    public R synchronize(){
        return tagService.synchronize();
    }

    @GetMapping("/export")
    @ApiOperation(value = "标签导出", notes = "tagid，多个标签，标签id以逗号分隔")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tagid", required = true, value = "标签id", paramType = "query", dataType = "String")
    })
    public void export(@RequestParam("tagid") String tagid, HttpServletResponse response) throws Exception {
        List<ExportTagEntity> exportTagEntityList = tagService.export(tagid);
        EasyPoiExcelUtil.exportExcel(exportTagEntityList,"标签信息统计","标签信息",ExportTagEntity.class,"标签信息.xlsx",response);
    }

    @PostMapping("/upload")
    @ApiOperation(value = "标签导入", notes = "")
    public R upload(MultipartFile file, HttpServletRequest request) throws Exception {
        ImportParams params = new ImportParams();
        params.setTitleRows(0);
        params.setHeadRows(1);
        List<ExportTagEntity> list;
        try {
            // excel的数据
            list = WebExcelUtil.importExcel(file, 1, 1, ExportTagEntity.class);
            return  tagService.save(list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("导入失败");
        }
    }

    @GetMapping("/export/template")
    @ApiOperation(value = "标签模板导出", notes = "")
    public void exportTemplate(HttpServletResponse response) throws Exception {
        List<ExportTagTemplateEntity> exportTagEntityList = new ArrayList<ExportTagTemplateEntity>();
        ExportTagTemplateEntity exportTagTemplateEntity = new ExportTagTemplateEntity();
        exportTagTemplateEntity.setUserid("zhangsan");
        exportTagTemplateEntity.setType("成员");
        exportTagTemplateEntity.setTagname("标签1");
        exportTagEntityList.add(exportTagTemplateEntity);
        EasyPoiExcelUtil.exportExcel(exportTagEntityList,"标签导入模板","标签模板",ExportTagTemplateEntity.class,"标签模板.xlsx",response);
    }
}
